TARGET_NAME = ZINO_AT32F413RCT7_BOOTLOOADER
TARGET_BOARD = FUNPALY_DRONE
SERIAL_PROT = COM3
MCPU = AT32F413RCT7
BUILD_DATE := $(shell date +"%Y%m%d")
MAIN_VERSION := 1
SUB_VERSION := 0
BUILD_NUMBER := 0
TARGET_VERSION = V$(MAIN_VERSION).$(SUB_VERSION).$(BUILD_NUMBER)
BUILD_VERSION += $(TARGET_VERSION)_$(BUILD_DATE)
TARGET_ELF = ./build/$(TARGET_NAME)_$(BUILD_VERSION).elf
TARGET_BIN = ./build/$(TARGET_NAME)_$(BUILD_VERSION).bin

targets: $(TARGET_BIN) $(TARGET_ELF) $(nOBJS)

linkerScript = ./libraries/cmsis/cm4/device_support/startup/gcc/linker/AT32F413xC_FLASH.ld

lib_inc = \
	./libraries/cmsis/cm4/core_support \
	./libraries/cmsis/cm4/device_support \
	./libraries/cmsis/cm4/device_support/startup \
	./libraries/drivers/inc \
	./project/CRC

libSrcDir = ./libraries/drivers/src
lib_src = \
	./libraries/cmsis/cm4/device_support/system_at32f413.c \
	$(libSrcDir)/at32f413_acc.c \
	$(libSrcDir)/at32f413_adc.c \
	$(libSrcDir)/at32f413_bpr.c \
	$(libSrcDir)/at32f413_can.c \
	$(libSrcDir)/at32f413_crc.c \
	$(libSrcDir)/at32f413_crm.c \
	$(libSrcDir)/at32f413_debug.c \
	$(libSrcDir)/at32f413_dma.c \
	$(libSrcDir)/at32f413_exint.c \
	$(libSrcDir)/at32f413_flash.c \
	$(libSrcDir)/at32f413_gpio.c \
	$(libSrcDir)/at32f413_i2c.c \
	$(libSrcDir)/at32f413_misc.c \
	$(libSrcDir)/at32f413_pwc.c \
	$(libSrcDir)/at32f413_rtc.c \
	$(libSrcDir)/at32f413_sdio.c \
	$(libSrcDir)/at32f413_spi.c \
	$(libSrcDir)/at32f413_tmr.c \
	$(libSrcDir)/at32f413_usart.c \
	$(libSrcDir)/at32f413_usb.c \
	$(libSrcDir)/at32f413_wdt.c \
	$(libSrcDir)/at32f413_wwdt.c

INC = $(lib_inc) ./project/inc
srcDir = ./project/src
src_c= \
	$(lib_src) \
	$(srcDir)/at32f413_clock.c \
	$(srcDir)/at32f413_int.c \
	$(srcDir)/main.c \
	$(srcDir)/bootloader.c \
	./project/CRC/crc.c \

INCLUDES = $(addprefix -I", $(addsuffix ", $(INC)))
DEPS = $(patsubst ./%,./build/%,$(patsubst %.c, %.d, $(src_c)))
OBJS = $(patsubst ./%,./build/%,$(patsubst %.c, %.o, $(src_c)))
SRC += $(src_c)
SRC +=  ./libraries/cmsis/cm4/device_support/startup/gcc/startup_at32f413.s
DEPS += ./build/libraries/cmsis/cm4/device_support/startup/gcc/startup_at32f413.d
OBJS += ./build/libraries/cmsis/cm4/device_support/startup/gcc/startup_at32f413.o
#-g -gdwarf-2
c_flags = \
	-mcpu=cortex-m4 -mthumb -mfloat-abi=soft \
	-Os -ffunction-sections -fdata-sections \
	-Wall  -DSOC_FAMILY_AT32 \
	-D$(MCPU) \
	-lm -DARM_MATH_CM4 \
	$(INCLUDES) \
	-std=gnu11 -MMD -MP -MD -MF "$(@:%.o=%.d)" -MT"$(@)"
DATE_TIME = $(shell date +"%Y-%m-%d-%H:%M:%S")
asm_flags = \
	-mcpu=cortex-m4 -mthumb -mfloat-abi=soft \
	-Os -ffunction-sections -fdata-sections -Wall -x assembler-with-cpp \
	-I"./" \
	-Xassembler -mimplicit-it=thumb -MMD -MP

-include $(DEPS)
./build/%.o: %.c
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $< -c -o $@ $(c_flags)
./build/%.o: %.s
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $(asm_flags) -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"
./build/%.o: %.S
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $(asm_flags) -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"
m_ldflags= \
	-mcpu=cortex-m4 \
	-mthumb -mfloat-abi=soft -Os \
	-Wall \
	-T "$(linkerScript)" \
	-Wl,-Map,"./build/$(TARGET_NAME).map" \
	-Wl,--no-warn-rwx-segments \
	-Xlinker --gc-sections \
	-Xlinker --cref -Wl,--print-memory-usage
$(TARGET_ELF): $(OBJS)
	@ echo "$(DATE_TIME) Linking everyting together... $@"
	@ arm-none-eabi-gcc -o $(TARGET_ELF) -Wl,--whole-archive $(OBJS) -Wl,--no-whole-archive $(m_ldflags) -lc -lm
$(TARGET_BIN): $(TARGET_ELF)
	@ echo "$(DATE_TIME) Create binary file... $@"
	@ arm-none-eabi-objcopy -O binary $(TARGET_ELF) $(TARGET_BIN)
cl:
	-$(RM) $(OBJS)$(TARGET_ELF)$(DEPS)
	-@echo ' '
dl: $(TARGET_BIN) $(TARGET_ELF)
	@ echo "$(DATE_TIME) Downloading $(TARGET_BIN) to board..."
	chcp 65001
	@ ./tool/at32_isp_tool_win_v3.5.exe \
	--comport $(SERIAL_PROT) 256000 --isp --getCommands \
	--getVersion --getDeviceId \
	--accessUnprotect \
	--eraseAndProgramUnprotect \
	--resetDevice \
	--isp \
	--erase bank1 \
	--writefile $(TARGET_BIN) 0x08000000 \
	--resetDevice \
	--isp --getCommands --getVersion --getDeviceId \
	--readMemory 1FFFF800 48 \
	--getVersion --getDeviceId \
	--getslibstatus \
	--writeMemory 1FFFF800 A5 5A FF 00 FF 00 FF 00 FF 00 FF 00 FF 00 FF 00 FE 01 FF 00 FF 00 FF 00 FF 00 FF 00 FF 00 FF 00 FF 00 FF 00 FF 00 FF 00 FF 00 FF 00 FF 00 FF 00

